#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;
use Code;
use Logs;
use Temp;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    logs => 日志对象

    uuid => version的uuid

#只有使用uuid来构建的时候才允许send

=cut

return sub
{
    my %param = @_;

    my ( $db, $logs, $uuid, $projectid, $version, $follow_up, $follow_up_ticketid ) 
        = @param{qw( db logs uuid projectid version follow_up follow_up_ticketid )};

    print "=" x 75, "\n";
    print "follow_up($follow_up)...\n";

    my $TICKET = '';

    if( $follow_up_ticketid )
    {
        my $x = eval{ $db->query( "select ticket,type from ticket where id='$follow_up_ticketid'" )};
        $logs->die( "get data from ticket fail:$@" ) if $@;
        if( $x->[0][1] eq 'UsernamePassword' )
        {
            my ( $user, $pass ) = split /_:separator:_/, $x->[0][0] if @$x;
            if( $user )
            {
                $logs->die( "ticket format error" ) if $user =~ /'/ || $pass=~ /'/;
                $TICKET = "TUSERNAME='$user' TPASSWORD='$pass'";
            }
        }
        else
        {
            my $ticketfile = Temp->new( chmod => 0600 )->dump( $x->[0][0] );
            $TICKET = "TICKETFILE=$ticketfile";
        }
    }

    $logs = Logs->new( 'code.build.send.follow_up' ) unless $logs;
    $logs->die( "format error" ) unless $follow_up =~ /^[a-zA-Z0-9][a-zA-Z0-9\._,\;\- \/\:=]*$/ &&  $follow_up !~ /\.\./;

    map{
        $_ =~ s/^ +//;
        $logs->die( "run build.send.follow_up fail:$!" ) if system "VERSION=$version PROJECTID=$projectid $TICKET /data/Software/mydan/CI/follow_up/$_";
    }split /;/, $follow_up;

}
